package com.qcxk.yuqian.rtu.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.qcxk.yuqian.rtu.entity.Area;
import com.qcxk.yuqian.rtu.mapper.AreaMapper;
import com.qcxk.yuqian.rtu.service.IAreaService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.qcxk.yuqian.system.component.QueryWrapperConverter;

import java.util.ArrayList;
import java.util.List;

/**
 * @auther lp
 * @create 2021-11-15 03:16:56
 * @describe 服务实现类
 */
@Service
public class AreaServiceImpl extends ServiceImpl<AreaMapper, Area> implements IAreaService {
    @Autowired
    private AreaMapper areaMapper;

    @Override
    public Page queryAreaPage(Page page, Area area) {

        QueryWrapper<Area> areaQueryWrapper = new QueryWrapper<>();
        QueryWrapperConverter.converter(areaQueryWrapper, area);

        areaQueryWrapper.orderByDesc("ctime");
        areaMapper.selectPage(page, areaQueryWrapper);
        return page;
    }

    @Override
    public boolean deleteAreaById(Area area) {
        int re = areaMapper.deleteById(area.getId());
        return re == 1 ? true : false;
    }

    @Override
    public List<Area> getChildList(String id, String isContainUser) {
        List<Area> areaList = areaMapper.selectChildList(id);
        if (areaList != null & !areaList.isEmpty()) {
//            if(StringUtils.isNotBlank(isContainUser)){
//                if(isContainUser.equals("0")){//包含用户
//                    areaList.stream().forEach(e->{
//                        List<User> tempList = initRedisCache.getRedisUserByAreaId(e.getId());
//                        JSONArray jsonArray = new JSONArray();
//                        for(int i=0;i<tempList.size();i++){
//                            User item = tempList.get(i);
//                            JSONObject json = new JSONObject();
//                            json.put("id",item.getId());
//                            json.put("departName",item.getFullName());
//                            json.put("parentId",e.getPId());
//                            json.put("type","user");
//                            jsonArray.add(json);
//                        }
////                        e.setUserList(jsonArray);
//                    });
//                }
//            }
            areaList = getTree(areaList, Integer.parseInt(id));
        }
        return areaList;
    }

    private List<Area> getTree(List<Area> list, long parentId) {
        //获取所有子节点
        List<Area> childTreeList = getChildTree(list, parentId);
        for (Area area : childTreeList) {
            area.setChildList(getTree(list, area.getId()));
        }
        return childTreeList;
    }

    private List<Area> getChildTree(List<Area> list, long id) {
        List<Area> childTree = new ArrayList<>();
        for (Area area : list) {
            if (area.getPId() == id) {
                childTree.add(area);
            }
        }
        return childTree;
    }
}
